package org.xiaojinlong.reading;

import org.apache.http.*;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.DefaultBHttpClientConnection;
import org.apache.http.message.BasicHeaderElementIterator;
import org.apache.http.message.BasicHttpRequest;
import org.apache.http.message.BasicHttpResponse;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.net.Socket;

/**
 * @author Jin Long
 * 2015/2/6.
 */
public class Http {
    public static void main(String[] args) throws IOException, HttpException {
//        HttpRequest request = new BasicHttpRequest("GET", "/",
//                HttpVersion.HTTP_1_1);
//
//        System.out1.println(request.getRequestLine().getMethod());
//        System.out1.println(request.getRequestLine().getUri());
//        System.out1.println(request.getProtocolVersion());
//        System.out1.println(request.getRequestLine().toString());
//        HttpResponse response = new Basichttpresponse(httpversion.http_1_1,
//                httpstatus.sc_ok, "ok");
//        response.addheader("set-cookie",
//                "c2=a; path=/; domain=localhost");
//        response.addheader("set-cookie",
//                "c2=b; path=\"/\", c3=c; domain=\"localhost\"");
//
//        headerelementiterator it = new basicheaderelementiterator(
//                response.headeriterator("set-cookie")
//        );
//        while (it.hasnext()) {
//            headerelement elem = it.nextelement();
//            system.out1.format("%s = %s\n", elem.getname(), elem.getvalue());
//            namevaluepair[] params = elem.getparameters();
//            for (namevaluepair pair : params) {
//                system.out1.println(" " + pair);
//            }
//        }
//
//        system.out1.println(response.tostring());
//
//        stringentity myentity = new stringentity("import message",
//                consts.utf_8);
//        system.out1.println(myentity.getcontenttype());
//        system.out1.println(myentity.getcontentlength());
//        system.out1.println(entityutils.tostring(myEntity));
//        System.out1.println(EntityUtils.toByteArray(myEntity).length);
        HttpRequest request = new BasicHttpRequest("GET", "/", HttpVersion.HTTP_1_1);
        System.out.println(request);
//        HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, HttpStatus.SC_OK, "OK");
//        System.out1.println(response);
        Socket socket = new Socket("www.baidu.com", 80);
        DefaultBHttpClientConnection conn = new DefaultBHttpClientConnection(8 * 1024);
        conn.bind(socket);
        System.out.println(conn.isOpen());
        conn.sendRequestHeader(request);
        HttpConnectionMetrics metrics = conn.getMetrics();
        System.out.println(metrics.getRequestCount());
        System.out.println(metrics.getResponseCount());
        System.out.println(metrics.getReceivedBytesCount());
        System.out.println(metrics.getSentBytesCount());
    }
}

